﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7" />
<title>List Patient</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>

<script>
	$(document).ready(function(){
		var datepicker_CurrentInput;  
        $.datepicker.setDefaults({ showButtonPanel: true, closeText: 'clear', beforeShow: function (input, inst) { datepicker_CurrentInput = input; } });  
        $(".ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
        });  
		jQuery("#birth").datepicker({
			dateFormat:'dd-mm-yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
	});
</script>


</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>

		
		<?php 
			ini_set('default_charset', "UTF-8");
			$ini_array = parse_ini_file("config/config.ini");
			
	
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			mysqli_set_charset($conn, "utf8");
			date_default_timezone_set($ini_array["default_timezone"]);
			
			$getPatientSql = "select Patient_no, First_name, Surname, Date_birth, address from patient ";
		

			$hasParameter = false;
			$patientNoFormatError = false;
			if( isset($_POST["patientNo"]) && $_POST["patientNo"] != "" ){
				if( preg_match("/^\d+$/", $_POST["patientNo"]) == true ){
					if( $hasParameter == false ){
						$getPatientSql .= " where ";
					}else{
						$getPatientSql .= " and ";
					}
					
					$getPatientSql .= " Patient_no=".$_POST["patientNo"]." ";
					$hasParameter = true;
				}else{
					$patientNoFormatError = true;		
				}
				
			}
			
			if( isset($_POST["firstName"]) && $_POST["firstName"] != "" ){
				if( $hasParameter == false ){
					$getPatientSql .= " where ";
				}else{
					$getPatientSql .= " and ";
				}
			
				$getPatientSql .= "  First_name like '%".$_POST["firstName"]."%' ";
				$hasParameter = true;
			
			}
			
			if( isset($_POST["surname"]) && $_POST["surname"] != "" ){
				if( $hasParameter == false ){
					$getPatientSql .= " where ";
				}else{
					$getPatientSql .= " and ";
				}
			
				$getPatientSql .= "  Surname like '%".$_POST["surname"]."%' ";
				$hasParameter = true;
			
			}
			
			if( isset($_POST["birth"]) && $_POST["birth"] != "" ){
				if( $hasParameter == false ){
					$getPatientSql .= " where ";
				}else{
					$getPatientSql .= " and ";
				}
				
				$getPatientSql .= " Date_birth=str_to_date('".$_POST["birth"]."', '%d-%m-%Y')";
				$hasParameter = true;
			}

			$patientResult = mysqli_query($conn, $getPatientSql);
			
		?>
		<div id="search">
			<form id="searchForm" name="searchForm" method="post" action="list_patient.php">
				<table style="width:1190px">
					<tr>
					<td><label>Patient No.:</label><input type="text" name="patientNo" id="patientNo" value="<?php if( isset($_POST["patientNo"]))echo $_POST["patientNo"]; ?>" style="width: 80px"/></td>
					<td><label>First Name:</label><input type="text" name="firstName" id="firstName" value="<?php if( isset($_POST["firstName"])) echo $_POST["firstName"]?>" style="width: 80px"/> </td>
					<td><label>Surname:</label><input type="text" name="surname" id="surname" value="<?php if( isset($_POST["surname"])) echo $_POST["surname"]?>" style="width: 80px"/> </td>
					<td><label>DOB</label><input type="text" name="birth" id="birth" value="<?php if( isset($_POST["birth"])) echo $_POST["birth"]?>" style="width:80px;" /></td>
					<td>
						<input type="submit" name="search" value="query"/>
						<input type="button" name="new_patient" value="New_Patient" onclick="window.location.href='patient_info.php'"/>
						<label><?php if( $patientNoFormatError == true ) echo "patient No. error!!";?></label>
					</td>
					</tr>
				</table>
				
			</form>
		</div>
		<div id="main_content" style="text-align:center">
			<div id="title"><h2>List of Patients</h2></div>
			<div style="width:100px;display:inline-block;"><label>Patient No</label></div>
			<div style="width:150px;display:inline-block;"><label>First Name</label></div>
			<div style="width:150px;display:inline-block;"><label>Surname</label></div>
			<div style="width:150px;display:inline-block;"><label>date birth</label></div>
			<div style="width:300px;display:inline-block;"><label>address</label></div>
			<div style="width:60px;display:inline-block;"><label>Operate</label></div>
			<div style="width:80px;display:inline-block;"><label>Account</label></div>
			
			
		
			<div id="patientList" style="width:1020px;margin:0px auto;overflow:auto;max-height:600px;">
				<table style="width:1000px;">
					<?php 
						while( $row = mysqli_fetch_array($patientResult)){
							$patientNo = $row["Patient_no"];
							$firstName = $row["First_name"];
							if( strlen($firstName) > 15 ){
								$firstName = substr($firstName, 0, 15);
							}
							
							$surname = $row["Surname"];
							if( strlen($surname) > 15 ){
								$surname = substr($surname, 0, 15);
							}
							
							$dateBirth = $row["Date_birth"];
							if( strcmp($dateBirth, "") != 0 ){	
								$dateBirth = date("d-m-Y", strtotime($dateBirth));	
							}
							
							
							$address = $row["address"];
							if( strlen($address) > 35 ){
								$address = substr($address, 0, 35);
							}
							
					?>
					<tr>
						<td style="width:10%;"><?php echo $patientNo?></td>
						<td style="width:15%;"><?php echo $firstName?></td>
						<td style="width:15%;"><?php echo $surname?></td>
						<td style="width:15%;"><?php echo $dateBirth?></td>
						<td style="width:30%;"><?php echo $address?></td>
						<td style="width:6%;"><a href="patient_info.php?patient_no=<?php echo $patientNo?>">view</a></td>
						<td style="width:8%;"><a href="patient_account.php?patientNo=<?php echo $patientNo?>">account</a></td>
					</tr>
					<?php 
						}
					?>
				</table>
			</div>
			<input type="button" value="return" onclick="window.location.href='index.php'"/>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
